export default function OperationHook(state: Base.GeneraDataType) {
  const { passData, tableRef, tableData } = state;
  const selectChangeEvent = (e: any) => {
    const checkedRows = [];
    checkedRows.push(e.row.id);
    if (e.row.children && e.row.children.length) getAllId(e.row.children, checkedRows);
    if (e.checked) {
      tableRef.value.setCheckboxRow(checkedRows, true);
    } else {
      tableRef.value.setCheckboxRow(checkedRows, false);
    }
    passData.value.clients = e.$table.getCheckboxRecords().map((t: any) => {
      return {
        id: t.id,
        pid: t.pid
      };
    });
  };
  const getAllId = (target: Array<Base.GeneraDataType>, result: Array<Base.GeneraDataType>): any => {
    for (let i = 0; i < target.length; i++) {
      const source = target[i];
      result.push(source);
      if (source.children && source.children.length) getAllId(source.children, result);
    }
  };
  const selectAllHandle = () => {
    tableRef.value.setCheckboxRow(tableData.value, true);
    passData.value.clients = tableRef.value.getCheckboxRecords().map((t: any) => {
      return {
        id: t.id,
        pid: t.pid
      };
    });
  };
  const callSelectAllHandle = () => {
    tableRef.value.setCheckboxRow(tableData.value, false);
    passData.value.clients = [];
  };
  return {
    selectChangeEvent,
    selectAllHandle,
    callSelectAllHandle
  };
}
